package com.service;

import com.dao.OrderMapper;
import com.model.AppSystem;
import com.model.FspAppSystem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

/**
 * @ Author xsd
 * @ crate time 2020/5/27 11:03
 * @ 描述
 * zj: execute(new Runnable()--》run()
 *     new FutureTask<Integer>(new Callable<Integer>()--》call()
 */
@Service
@Transactional
// Propagation propagation() default Propagation.REQUIRED; 默认，有事务加入-无开启
@Slf4j
public class OrderServiceIml implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private Executor threadTool;

    @Override
    public int insertS(final FspAppSystem fspAppSystem) throws Exception{
        FspAppSystem system=new FspAppSystem();
        system.setId("222222222");
        System.out.println(orderMapper.insertS(system));
        //1、测试线程池 无法事务控制
        FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
            public Integer call() throws Exception {
                log.info("子线程插入数据。。。。");
                int restult;
                restult = orderMapper.insertS(fspAppSystem);
                System.out.println("插入数据结果："+restult);
                return restult;
            }
        });
        // 提交任务
        threadTool.execute(task);
        //阻塞-拿到结果
        return task.get();
    }

    @Override
    public int delete(String id) {
        int result=orderMapper.delete(id);
        return result;
    }

    /**
     * 同一个 sqlSession 一级缓存，默认启动
     * @return
     */
    @Override
    public List<AppSystem> queryAll() {
//        List<AppSystem> map= orderMapper.queryAll();
        List<AppSystem> systemList= orderMapper.queryAll();
        return systemList;
    }

    public void threadInsert(final FspAppSystem fspAppSystem){
        threadTool.execute(new Runnable() {
            public void run() {
                log.info("子线程插入数据。。。。");
                int restult;
                restult = orderMapper.insertS(fspAppSystem);
                System.out.println("插入数据结果："+restult);
            }
        });
    }
}
